<template>
  <el-dialog
    :title="!dataForm.uid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户名" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="passWord">
      <el-input v-model="dataForm.passWord" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="性别(1:男2:女)" prop="gender">
      <el-input v-model="dataForm.gender" placeholder="性别(1:男2:女)"></el-input>
    </el-form-item>
    <el-form-item label="个人头像" prop="avatar">
      <el-input v-model="dataForm.avatar" placeholder="个人头像"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
    </el-form-item>
    <el-form-item label="出生年月日" prop="birthday">
      <el-input v-model="dataForm.birthday" placeholder="出生年月日"></el-input>
    </el-form-item>
    <el-form-item label="手机" prop="mobile">
      <el-input v-model="dataForm.mobile" placeholder="手机"></el-input>
    </el-form-item>
    <el-form-item label="邮箱验证码" prop="validCode">
      <el-input v-model="dataForm.validCode" placeholder="邮箱验证码"></el-input>
    </el-form-item>
    <el-form-item label="自我简介最多150字" prop="summary">
      <el-input v-model="dataForm.summary" placeholder="自我简介最多150字"></el-input>
    </el-form-item>
    <el-form-item label="登录次数" prop="loginCount">
      <el-input v-model="dataForm.loginCount" placeholder="登录次数"></el-input>
    </el-form-item>
    <el-form-item label="最后登录时间" prop="lastLoginTime">
      <el-input v-model="dataForm.lastLoginTime" placeholder="最后登录时间"></el-input>
    </el-form-item>
    <el-form-item label="最后登录IP" prop="lastLoginIp">
      <el-input v-model="dataForm.lastLoginIp" placeholder="最后登录IP"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="昵称" prop="nickName">
      <el-input v-model="dataForm.nickName" placeholder="昵称"></el-input>
    </el-form-item>
    <el-form-item label="资料来源" prop="source">
      <el-input v-model="dataForm.source" placeholder="资料来源"></el-input>
    </el-form-item>
    <el-form-item label="平台uuid" prop="uuid">
      <el-input v-model="dataForm.uuid" placeholder="平台uuid"></el-input>
    </el-form-item>
    <el-form-item label="QQ号" prop="qqNumber">
      <el-input v-model="dataForm.qqNumber" placeholder="QQ号"></el-input>
    </el-form-item>
    <el-form-item label="微信号" prop="weChat">
      <el-input v-model="dataForm.weChat" placeholder="微信号"></el-input>
    </el-form-item>
    <el-form-item label="职业" prop="occupation">
      <el-input v-model="dataForm.occupation" placeholder="职业"></el-input>
    </el-form-item>
    <el-form-item label="评论状态 1:正常 0:禁言" prop="commentStatus">
      <el-input v-model="dataForm.commentStatus" placeholder="评论状态 1:正常 0:禁言"></el-input>
    </el-form-item>
    <el-form-item label="ip来源" prop="ipSource">
      <el-input v-model="dataForm.ipSource" placeholder="ip来源"></el-input>
    </el-form-item>
    <el-form-item label="浏览器" prop="browser">
      <el-input v-model="dataForm.browser" placeholder="浏览器"></el-input>
    </el-form-item>
    <el-form-item label="操作系统" prop="os">
      <el-input v-model="dataForm.os" placeholder="操作系统"></el-input>
    </el-form-item>
    <el-form-item label="是否开启邮件通知 1:开启 0:关闭" prop="startEmailNotification">
      <el-input v-model="dataForm.startEmailNotification" placeholder="是否开启邮件通知 1:开启 0:关闭"></el-input>
    </el-form-item>
    <el-form-item label="用户标签：0：普通用户，1：管理员，2：博主 等" prop="userTag">
      <el-input v-model="dataForm.userTag" placeholder="用户标签：0：普通用户，1：管理员，2：博主 等"></el-input>
    </el-form-item>
    <el-form-item label="是否通过加载校验【0 未通过，1 已通过】" prop="loadingValid">
      <el-input v-model="dataForm.loadingValid" placeholder="是否通过加载校验【0 未通过，1 已通过】"></el-input>
    </el-form-item>
    <el-form-item label="积分" prop="credits">
      <el-input v-model="dataForm.credits" placeholder="积分"></el-input>
    </el-form-item>
    <el-form-item label="个人中心背景图片" prop="backgroundFileUid">
      <el-input v-model="dataForm.backgroundFileUid" placeholder="个人中心背景图片"></el-input>
    </el-form-item>
    <el-form-item label="编辑器模式，(0：富文本编辑器CKEditor，1：markdown编辑器Veditor)" prop="editorModel">
      <el-input v-model="dataForm.editorModel" placeholder="编辑器模式，(0：富文本编辑器CKEditor，1：markdown编辑器Veditor)"></el-input>
    </el-form-item>
    <el-form-item label="是否删除:0否 1是" prop="isDelete">
      <el-input v-model="dataForm.isDelete" placeholder="是否删除:0否 1是"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          uid: 0,
          userName: '',
          passWord: '',
          gender: '',
          avatar: '',
          email: '',
          birthday: '',
          mobile: '',
          validCode: '',
          summary: '',
          loginCount: '',
          lastLoginTime: '',
          lastLoginIp: '',
          status: '',
          createTime: '',
          updateTime: '',
          nickName: '',
          source: '',
          uuid: '',
          qqNumber: '',
          weChat: '',
          occupation: '',
          commentStatus: '',
          ipSource: '',
          browser: '',
          os: '',
          startEmailNotification: '',
          userTag: '',
          loadingValid: '',
          credits: '',
          backgroundFileUid: '',
          editorModel: '',
          isDelete: ''
        },
        dataRule: {
          userName: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          passWord: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          gender: [
            { required: true, message: '性别(1:男2:女)不能为空', trigger: 'blur' }
          ],
          avatar: [
            { required: true, message: '个人头像不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '邮箱不能为空', trigger: 'blur' }
          ],
          birthday: [
            { required: true, message: '出生年月日不能为空', trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '手机不能为空', trigger: 'blur' }
          ],
          validCode: [
            { required: true, message: '邮箱验证码不能为空', trigger: 'blur' }
          ],
          summary: [
            { required: true, message: '自我简介最多150字不能为空', trigger: 'blur' }
          ],
          loginCount: [
            { required: true, message: '登录次数不能为空', trigger: 'blur' }
          ],
          lastLoginTime: [
            { required: true, message: '最后登录时间不能为空', trigger: 'blur' }
          ],
          lastLoginIp: [
            { required: true, message: '最后登录IP不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          nickName: [
            { required: true, message: '昵称不能为空', trigger: 'blur' }
          ],
          source: [
            { required: true, message: '资料来源不能为空', trigger: 'blur' }
          ],
          uuid: [
            { required: true, message: '平台uuid不能为空', trigger: 'blur' }
          ],
          qqNumber: [
            { required: true, message: 'QQ号不能为空', trigger: 'blur' }
          ],
          weChat: [
            { required: true, message: '微信号不能为空', trigger: 'blur' }
          ],
          occupation: [
            { required: true, message: '职业不能为空', trigger: 'blur' }
          ],
          commentStatus: [
            { required: true, message: '评论状态 1:正常 0:禁言不能为空', trigger: 'blur' }
          ],
          ipSource: [
            { required: true, message: 'ip来源不能为空', trigger: 'blur' }
          ],
          browser: [
            { required: true, message: '浏览器不能为空', trigger: 'blur' }
          ],
          os: [
            { required: true, message: '操作系统不能为空', trigger: 'blur' }
          ],
          startEmailNotification: [
            { required: true, message: '是否开启邮件通知 1:开启 0:关闭不能为空', trigger: 'blur' }
          ],
          userTag: [
            { required: true, message: '用户标签：0：普通用户，1：管理员，2：博主 等不能为空', trigger: 'blur' }
          ],
          loadingValid: [
            { required: true, message: '是否通过加载校验【0 未通过，1 已通过】不能为空', trigger: 'blur' }
          ],
          credits: [
            { required: true, message: '积分不能为空', trigger: 'blur' }
          ],
          backgroundFileUid: [
            { required: true, message: '个人中心背景图片不能为空', trigger: 'blur' }
          ],
          editorModel: [
            { required: true, message: '编辑器模式，(0：富文本编辑器CKEditor，1：markdown编辑器Veditor)不能为空', trigger: 'blur' }
          ],
          isDelete: [
            { required: true, message: '是否删除:0否 1是不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.uid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.uid) {
            this.$http({
              url: this.$http.adornUrl(`/user/user/info/${this.dataForm.uid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userName = data.user.userName
                this.dataForm.passWord = data.user.passWord
                this.dataForm.gender = data.user.gender
                this.dataForm.avatar = data.user.avatar
                this.dataForm.email = data.user.email
                this.dataForm.birthday = data.user.birthday
                this.dataForm.mobile = data.user.mobile
                this.dataForm.validCode = data.user.validCode
                this.dataForm.summary = data.user.summary
                this.dataForm.loginCount = data.user.loginCount
                this.dataForm.lastLoginTime = data.user.lastLoginTime
                this.dataForm.lastLoginIp = data.user.lastLoginIp
                this.dataForm.status = data.user.status
                this.dataForm.createTime = data.user.createTime
                this.dataForm.updateTime = data.user.updateTime
                this.dataForm.nickName = data.user.nickName
                this.dataForm.source = data.user.source
                this.dataForm.uuid = data.user.uuid
                this.dataForm.qqNumber = data.user.qqNumber
                this.dataForm.weChat = data.user.weChat
                this.dataForm.occupation = data.user.occupation
                this.dataForm.commentStatus = data.user.commentStatus
                this.dataForm.ipSource = data.user.ipSource
                this.dataForm.browser = data.user.browser
                this.dataForm.os = data.user.os
                this.dataForm.startEmailNotification = data.user.startEmailNotification
                this.dataForm.userTag = data.user.userTag
                this.dataForm.loadingValid = data.user.loadingValid
                this.dataForm.credits = data.user.credits
                this.dataForm.backgroundFileUid = data.user.backgroundFileUid
                this.dataForm.editorModel = data.user.editorModel
                this.dataForm.isDelete = data.user.isDelete
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/user/${!this.dataForm.uid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'uid': this.dataForm.uid || undefined,
                'userName': this.dataForm.userName,
                'passWord': this.dataForm.passWord,
                'gender': this.dataForm.gender,
                'avatar': this.dataForm.avatar,
                'email': this.dataForm.email,
                'birthday': this.dataForm.birthday,
                'mobile': this.dataForm.mobile,
                'validCode': this.dataForm.validCode,
                'summary': this.dataForm.summary,
                'loginCount': this.dataForm.loginCount,
                'lastLoginTime': this.dataForm.lastLoginTime,
                'lastLoginIp': this.dataForm.lastLoginIp,
                'status': this.dataForm.status,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'nickName': this.dataForm.nickName,
                'source': this.dataForm.source,
                'uuid': this.dataForm.uuid,
                'qqNumber': this.dataForm.qqNumber,
                'weChat': this.dataForm.weChat,
                'occupation': this.dataForm.occupation,
                'commentStatus': this.dataForm.commentStatus,
                'ipSource': this.dataForm.ipSource,
                'browser': this.dataForm.browser,
                'os': this.dataForm.os,
                'startEmailNotification': this.dataForm.startEmailNotification,
                'userTag': this.dataForm.userTag,
                'loadingValid': this.dataForm.loadingValid,
                'credits': this.dataForm.credits,
                'backgroundFileUid': this.dataForm.backgroundFileUid,
                'editorModel': this.dataForm.editorModel,
                'isDelete': this.dataForm.isDelete
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
